package com.test.controller;

import com.test.pojo.Food;
import com.test.service.FoodService;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class FoodCtrl {
    @Resource
    private FoodService foodService;

    /**
     *获取所有的菜品
     */
    @RequestMapping("/getAllFood")
    public String getAllFood(Model model){
        List<Food> foodList = foodService.getAllFoodList();
        model.addAttribute("foodList",foodList);
        return "foodList";
    }
    /**
     *跳转到菜品更新页面
     */
    @GetMapping(value = "/updateFood")
    public String searchFoodByID(@RequestParam("foodId") int id, Model model){
        Food food = foodService.getFoodByID(id);
        if (food != null) {
            model.addAttribute("food",food);
            return "updateFood";
        }else {
            return "redirect:/getAllFood";
        }
    }
    /**删除菜品*/
    @RequestMapping(value ="/delFood/{id}",method = RequestMethod.GET)
    public String delFood(@PathVariable("id") int id, Model model){
        int num = foodService.deleteFood(id);
        System.out.println(num);
        if(num > 0){
            model.addAttribute("msg","菜品删除成功！");
        }else{
            model.addAttribute("msg","菜品删除失败！");
        }
        return "forward:/getAllFood";
    }
        /**
     * 转到添加页面
     * */
    @RequestMapping(value = "/saveFood")
    public  String saveFood(){
        return "saveFood";
    }

     /**
     * 添加菜品
     **/
    @PostMapping(value ="/doAddFoot")
    public String addFoot(Food food,Model model){

        int num = foodService.addFood(food);
        if(num > 0){
            model.addAttribute("msg","菜品【"+food.getFoodName()+"】添加成功！");
        }else{
            model.addAttribute("msg","菜品添加失败！");
        }
        return "forward:/getAllFood";
    }
}
